package com.mycloud.sample.service.impl;

import com.mycloud.sample.entity.User;
import com.mycloud.sample.repository.UserRepository;
import com.mycloud.sample.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

import lombok.extern.slf4j.Slf4j;

/**
 * @author : yinlin
 * @version : 1.0
 * @date : 2018/9/26 下午7:02
 * @Description :
 * @Copyright : Copyright (c) 2018
 * @Company : KeRuYun Technology(Beijing) Chengdu Co. Ltd.
 * @link : com.mycloud.sample.service.impl.UserServiceImpl
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public User save(User user) {
         return userRepository.save(user);
    }

    @Override
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public User findById(Long id) {
        Optional<User> optionalUser = userRepository.findById(id);
         return optionalUser.isPresent() ? optionalUser.get() : null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        User user = this.findById(id);
        if (user != null) {
            user.setUsable(false);
            userRepository.save(user);
        }
    }

    @Override
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public List<User> findAll() {
        return userRepository.findAll();
    }
}
